package com.haiyi.software.app;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.haiyi.software.app.basic.FragmentBasicSenderList;
import com.haiyi.software.tools.MyTools;
import com.haiyi.software.xmpp.event.XMPPConnectionState;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListDelegate;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView.PullToRefreshListAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class FragmentSenderList extends FragmentBasicSenderList implements
		PullToRefreshListDelegate {

	private View thisView;

	private PullToRefreshListView pull_refresh_list;

	private DisplayImageOptions options;

	private ImageLoader imageLoader;

	private MyTools tools = MyTools.getInstance();

	String photoString = "";

	private String str_km_distance = "";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreateView(inflater, container, savedInstanceState);

		thisView = inflater.inflate(R.layout.fragment_receive_gift_list, null);

		initComp();

		requestNetworkData();

		return thisView;
	}

	private void initComp() {

		// 图片控件
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();

		this.pull_refresh_list = (PullToRefreshListView) thisView
				.findViewById(R.id.pull_refresh_list);

		this.pull_refresh_list
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						requestNetworkData();
					}
				});
	}

	@Override
	protected void requestFinish() {
		// TODO 自动生成的方法存根
		this.pull_refresh_list.setPullToRefreshListDelegate(this);
		this.pull_refresh_list.setDataSource(data);
		this.pull_refresh_list.setAdapter(null);
		this.pull_refresh_list.getAdapterEx().notifyDataSetChanged();
		this.pull_refresh_list.onRefreshComplete();
	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根
		this.pull_refresh_list.onRefreshComplete();
		Toast.makeText(getActivity(), "网络错误，加载失败", Toast.LENGTH_SHORT).show();
	}

	@Override
	public View bindView(PullToRefreshListAdapter adapter, int position,
			View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;

		if (convertView == null) {

			convertView = adapter.getLayoutInflater().inflate(
					R.layout.item_gift_person, null);
			viewHolder = new ViewHolder();

			viewHolder.item_n_btn = (Button) convertView
					.findViewById(R.id.item_n_btn);

			viewHolder.item_n_name = (TextView) convertView
					.findViewById(R.id.item_n_name);

			viewHolder.item_n_photo = (ImageView) convertView
					.findViewById(R.id.item_n_photo);

			viewHolder.item_n_sex = (TextView) convertView
					.findViewById(R.id.item_n_sex);

			viewHolder.item_n_km_time = (TextView) convertView
					.findViewById(R.id.item_n_km_time);

			viewHolder.item_i_gold = (TextView) convertView
					.findViewById(R.id.item_i_gold);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.item_n_name
				.setText(data.get(position).get("username") != null ? data
						.get(position).get("username").toString() : "");

		viewHolder.item_n_sex.setBackgroundResource(tools.getSexBackground(data
				.get(position).get("usex").toString()));

		viewHolder.item_n_sex
				.setText(data.get(position).get("age") != null ? data
						.get(position).get("age").toString() : "");

		viewHolder.item_n_sex.setGravity(Gravity.CENTER_VERTICAL);
		viewHolder.item_n_sex.setPadding(40, 2, 5, 0);

		viewHolder.item_i_gold
				.setText(data.get(position).get("totalGold") != null ? data
						.get(position).get("totalGold").toString()
						+ "金币" : "");

		str_km_distance = data.get(position).get("juli") != null ? tools
				.beautifulDistance(data.get(position).get("juli").toString())
				: "0.00km";

		str_km_distance += " | ";

		str_km_distance += data.get(position).get("lastdatetime") != null ? tools
				.beautifulDateTime(data.get(position).get("lastdatetime")
						.toString()) : "";

		viewHolder.item_n_km_time.setText(str_km_distance);

		if (data.get(position).get("phosmall") != null) {
			photoString = str_server
					+ data.get(position).get("phosmall").toString();
		}
		imageLoader.displayImage(photoString, viewHolder.item_n_photo, options);

		return convertView;
	}

	private static class ViewHolder {
		ImageView item_n_photo;
		Button item_n_btn;
		TextView item_n_sex;
		TextView item_n_name;
		TextView item_n_km_time;
		TextView item_i_gold;
	}

	@Override
	public void XMPPConnectionStateListener(XMPPConnectionState args) {
		// TODO 自动生成的方法存根
		
	}

}
